clear all
set more off
set maxvar 20000

*Define directory with input data
cd "Q:"

*Load merged data
use se_analysis_data_1996_2018, clear

*Set weights equal to missing for those who don't appear in either earnings file
replace sswgts_cur=. if (merge_der_lcy==1 & merge_ser_lcy==1)
gen sswgts_cur_scaled=sswgts_cur/1000

replace sswgts_ev=. if (merge_der_lcy==1 & merge_ser_lcy==1)
gen sswgts_ev_scaled=sswgts_ev/1000

*Revisions to imputing

rename (*semp *bon *trad *2nd)(*se *ppt *tbc *othwork)

local vars "se wage ppt tbc othwork" 

foreach v in `vars' {
	gen `v'_inc2=.
	replace `v'_inc2=i`v' if inlist(if`v',1,2,3,4,5,6)

	gen `v'_inc2_imp_flag=0
	replace `v'_inc2_imp_flag=1 if inlist(if`v',2,3,4,5)
	gen `v'_inc2_imp_0_flag=0
	replace `v'_inc2_imp_0_flag=1 if ((inlist(if`v',2,3) & (`v'_inc_min<400 | `v'_inc_min==.)) | if`v'==5)
}

*Define HRS extensive margin income variables

gen any_hrs_e=.
replace any_hrs_e=1 if wage_inc2>0 & wage_inc2!=.
replace any_hrs_e=0 if wage_inc2==0
gen any_hrs_se=.
replace any_hrs_se=1 if se_inc2>=400 & se_inc2!=.
replace any_hrs_se=0 if se_inc2<400

gen hrs_se_only=.
replace hrs_se_only=0 if any_hrs_se==0 | any_hrs_e==1
replace hrs_se_only=1 if any_hrs_se==1 & any_hrs_e==0

gen hrs_e_only=.
replace hrs_e_only=0 if any_hrs_se==1 | any_hrs_e==0
replace hrs_e_only=1 if any_hrs_se==0 & any_hrs_e==1

gen hrs_se_and_e=.
replace hrs_se_and_e=0 if any_hrs_se==0 | any_hrs_e==0
replace hrs_se_and_e=1 if any_hrs_se==1 & any_hrs_e==1

gen no_hrs_se_or_e=.
replace no_hrs_se_or_e=0 if any_hrs_se==1 | any_hrs_e==1
replace no_hrs_se_or_e=1 if any_hrs_se==0 & any_hrs_e==0

gen hrs_se_or_e=.
replace hrs_se_or_e=0 if any_hrs_se==0 & any_hrs_e==0
replace hrs_se_or_e=1 if any_hrs_se==1 | any_hrs_e==1

*Define SSA extensive margin income variables

foreach v in cy lcy{

gen tot_se_earn_flag_`v'=1 if tot_se_earn_`v'==. & SE_`v'==1
gen non_se_earn_flag_`v'=1 if non_se_earn_`v'==. & (SE_`v'==2 | SE_`v'==3)

gen ser_se_flag_0_`v'=.
replace ser_se_flag_0_`v'=1 if tot_se_earn_`v'==. & ((SE_`v'==2 | SE_`v'==3) | EARN_`v'==0)

gen ser_ws_flag_0_`v'=.
replace ser_ws_flag_0_`v'=1 if non_se_earn_`v'==. & (SE_`v'==1 | EARN_`v'==0)

replace tot_se_earn_`v'=0 if tot_se_earn_`v'==. & EARN_`v'==0
replace non_se_earn_`v'=0 if non_se_earn_`v'==. & EARN_`v'==0

gen ser_se_flag_`v'=.
replace ser_se_flag_`v'=1 if tot_se_earn_`v'==. & SE_`v'==1

gen ser_ws_flag_`v'=.
replace ser_ws_flag_`v'=1 if non_se_earn_`v'==. & (SE_`v'==2 | SE_`v'==3)

replace tot_se_earn_`v'=EARN_`v' if tot_se_earn_`v'==. & SE_`v'==1
replace non_se_earn_`v'=0 if non_se_earn_`v'==. & SE_`v'==1

replace tot_se_earn_`v'=0 if tot_se_earn_`v'==. & (SE_`v'==2 | SE_`v'==3)
replace non_se_earn_`v'=EARN_`v' if non_se_earn_`v'==. & (SE_`v'==2 | SE_`v'==3)

gen any_ssa_se_ser_`v'=.
replace any_ssa_se_ser_`v'=0 if SE_`v'==2 | SE_`v'==3
replace any_ssa_se_ser_`v'=1 if SE_`v'==1 | SE_`v'==4

gen any_ssa_se_`v'=.
replace any_ssa_se_`v'=0 if SE_`v'==2 | SE_`v'==3 | (tot_se_earn_`v'<=0 & tot_se_earn_`v'!=.)
replace any_ssa_se_`v'=1 if SE_`v'==1 | SE_`v'==4 | (tot_se_earn_`v'>0 & tot_se_earn_`v'!=.) 

gen any_ssa_e_`v'=.
replace any_ssa_e_`v'=non_se_earn_`v' if non_se_earn_`v'>=0 & non_se_earn_`v'!=.
replace any_ssa_e_`v'=0 if (EARN_`v'==0 & tot_se_earn_`v'==. & SE_`v'!=.) | SE_`v'==1 | (non_se_earn_`v'<=0 & non_se_earn_`v'!=.)
replace any_ssa_e_`v'=1 if SE_`v'==3 | SE_`v'==4 | (non_se_earn_`v'>0 & non_se_earn_`v'!=.)
*drop tot_se_earn_`v' non_se_earn_`v' SE_`v'

gen ssa_se_only_`v'=any_ssa_se_`v'
replace ssa_se_only_`v'=0 if any_ssa_e_`v'==1

gen ssa_se_and_e_`v'=any_ssa_se_`v'
replace ssa_se_and_e_`v'=0 if any_ssa_e_`v'==0

gen ssa_e_only_`v'=any_ssa_e_`v'
replace ssa_e_only_`v'=0 if any_ssa_se_`v'==1

gen no_ssa_se_or_e_`v'=.
replace no_ssa_se_or_e_`v'=0 if any_ssa_se_`v'==1 | any_ssa_e_`v'==1
replace no_ssa_se_or_e_`v'=1 if any_ssa_se_`v'==0 & any_ssa_e_`v'==0

gen ssa_se_or_e_`v'=.
replace ssa_se_or_e_`v'=0 if any_ssa_se_`v'==0 & any_ssa_e_`v'==0
replace ssa_se_or_e_`v'=1 if any_ssa_se_`v'==1 | any_ssa_e_`v'==1

*Identify mismatched SE records
gen se_ssa_hrs_`v'=0
replace se_ssa_hrs_`v'=1 if any_ssa_se_`v'==1 & any_hrs_se==1
gen se_ssa_only_`v'=0
replace se_ssa_only_`v'=1 if any_ssa_se_`v'==1 & any_hrs_se==0
gen se_hrs_only_`v'=0
replace se_hrs_only_`v'=1 if any_ssa_se_`v'==0 & any_hrs_se==1
gen se_inc_`v'=0
replace se_inc_`v'=1 if se_ssa_hrs_`v'==1 | se_ssa_only_`v'==1 | se_hrs_only_`v'==1

*Identify mismatched wage records
gen e_ssa_hrs_`v'=0
replace e_ssa_hrs_`v'=1 if any_ssa_e_`v'==1 & any_hrs_e==1
gen e_ssa_only_`v'=0
replace e_ssa_only_`v'=1 if any_ssa_e_`v'==1 & any_hrs_e==0
gen e_hrs_only_`v'=0
replace e_hrs_only_`v'=1 if any_ssa_e_`v'==0 & any_hrs_e==1
}

*For Table 2
gen hrs_type=.
replace hrs_type=1 if hrs_se_only==1 
replace hrs_type=2 if hrs_e_only==1 
replace hrs_type=3 if hrs_se_and_e==1 
replace hrs_type=4 if no_hrs_se_or_e==1 

gen ssa_type=.
replace ssa_type=1 if ssa_se_only_lcy==1 
replace ssa_type=2 if ssa_e_only_lcy==1 
replace ssa_type=3 if ssa_se_and_e_lcy==1  
replace ssa_type=4 if no_ssa_se_or_e_lcy==1 

*Stats Table - For Appendix 4
/*
1- # linked to SER/DER for LCY with a positive SSA weight
2- # with any HRS SE inc (>$400) or WS inc
3- # with any HRS SE inc (>$400) 
4- # reported amount
5- # imputed from range >$400
6- # imputed from range with min=0
7- # with any HRS SE inc (<$400) 
8- # reported amount
9- # imputed from range with min=0
10- # with any HRS WS inc 
11- # reported amount
12- # imputed from closed range
13- # with any HRS other work inc (imputed to be >$0 if min=0, open max)
14- # with no HRS WS inc after imputation */

forvalues i=1/14 {
	gen statscats_`i'=0
	}
	
replace statscats_1=1 if waveyr>=2004 & waveyr<=2016 & wtcrnh>0 & wtcrnh!=. & (merge_ser_lcy==3 | merge_der_lcy==3) & sswgts_ev_scaled!=.
replace statscats_2=1 if statscats_1==1 & hrs_se_or_e==1
replace statscats_3=1 if statscats_1==1 & any_hrs_se==1
replace statscats_4=1 if statscats_3==1 & se_inc2_imp_flag==0
replace statscats_5=1 if statscats_3==1 & se_inc2_imp_flag==1 & se_inc2_imp_0_flag!=1 
replace statscats_6=1 if statscats_3==1 & se_inc2_imp_0_flag==1 
replace statscats_7=1 if statscats_1==1 & any_hrs_se==0 & se_inc!=. 
replace statscats_8=1 if statscats_7==1 & se_inc2_imp_flag==0
replace statscats_9=1 if statscats_7==1 & se_inc2_imp_0_flag==1 
replace statscats_10=1 if statscats_1==1 & any_hrs_e==1
replace statscats_11=1 if statscats_10==1 & wage_inc2_imp_flag==0
replace statscats_12=1 if statscats_10==1 & wage_inc2_imp_flag==1 
replace statscats_13=1 if statscats_1==1 & anyworkrelinc==1
replace statscats_14=1 if statscats_13==1 & any_hrs_e==0 

*Concordance

gen hrs_se_ssa_e=0 
gen hrs_se_no_ssa=0
gen hrs_se_and_e_ssa_e=0
gen no_ssa_se=0

replace hrs_se_ssa_e=1 if hrs_se_only==1 & ssa_e_only_lcy==1
replace hrs_se_no_ssa=1 if any_hrs_se==1 & no_ssa_se_or_e_lcy==1
replace hrs_se_and_e_ssa_e=1 if hrs_se_and_e==1 & ssa_e_only_lcy==1
replace no_ssa_se=1 if any_ssa_se_lcy==0

*For Table 3
gen mismatchtype=.
replace mismatchtype=1 if no_hrs_se_or_e==1 & ssa_se_only_lcy==1
replace mismatchtype=2 if hrs_e_only==1 & ssa_se_and_e_lcy==1
replace mismatchtype=3 if hrs_e_only==1 & ssa_se_only_lcy==1
replace mismatchtype=4 if hrs_se_only==1 & no_ssa_se_or_e_lcy==1
replace mismatchtype=5 if hrs_se_and_e==1 & ssa_e_only_lcy==1
replace mismatchtype=6 if hrs_se_only==1 & ssa_e_only_lcy==1
replace mismatchtype=7 if hrs_se_and_e==1 & no_ssa_se_or_e_lcy==1
replace mismatchtype=8 if no_hrs_se_or_e==1 & ssa_se_and_e_lcy==1

gen matchtype=.
replace matchtype=1 if hrs_se_only==1 & ssa_se_only_lcy==1
replace matchtype=2 if hrs_e_only==1 & ssa_e_only_lcy==1
replace matchtype=3 if hrs_se_and_e==1 & ssa_se_and_e_lcy==1
replace matchtype=4 if no_hrs_se_or_e==1 & no_ssa_se_or_e_lcy==1

gen mismatchtype_wage=.
replace mismatchtype_wage=1 if no_hrs_se_or_e==1 & ssa_se_and_e_lcy==1 
replace mismatchtype_wage=2 if no_hrs_se_or_e==1 & ssa_e_only_lcy==1
replace mismatchtype_wage=3 if hrs_se_only==1 & ssa_se_and_e_lcy==1
replace mismatchtype_wage=4 if hrs_e_only==1 & no_ssa_se_or_e_lcy==1

gen othermismatch=.
replace othermismatch=1 if hrs_se_and_e==1 & (ssa_se_only_lcy==1  | ssa_e_only_lcy==1)

gen matchcat=.
replace matchcat=1 if inlist(matchtype,1,2,3)
replace matchcat=2 if inlist(mismatchtype,1,8) | inlist(mismatchtype_wage,1,2)
replace matchcat=3 if inlist(mismatchtype,4,7) | mismatchtype_wage==4
replace matchcat=4 if inlist(mismatchtype,2,3,5,6) | inlist(mismatchtype_wage,3) | othermismatch==1

*For Figure 2
gen intcat=.
replace intcat=1 if inlist(matchtype,1,2,3)
replace intcat=2 if inlist(mismatchtype,1,4,7,8) | inlist(mismatchtype_wage,1,2,4)
replace intcat=3 if inlist(mismatchtype,3,6)
replace intcat=4 if inlist(mismatchtype,2,5)
replace intcat=5 if (hrs_se_and_e==1 & ssa_se_only_lcy==1) | inlist(mismatchtype_wage,3)

*For Table 4
gen roletype3=1 if matchtype==1
replace roletype3=2 if mismatchtype==6
replace roletype3=3 if mismatchtype_wage==3
replace roletype3=4 if mismatchtype==4
replace roletype3=5 if mismatchtype==3
replace roletype3=6 if matchtype==2
replace roletype3=7 if mismatchtype==2
replace roletype3=8 if mismatchtype_wage==4
replace roletype3=9 if hrs_se_and_e==1 & ssa_se_only_lcy==1  
replace roletype3=10 if mismatchtype==5
replace roletype3=11 if matchtype==3
replace roletype3=12 if mismatchtype==7
replace roletype3=13 if mismatchtype==1
replace roletype3=14 if mismatchtype_wage==2
replace roletype3=15 if mismatchtype_wage==1
replace roletype3=16 if matchtype==4

*Look at matches over time
gen consistent=.
replace consistent=1 if inlist(matchtype,1,2,3)
replace consistent=0 if mismatchtype!=. | mismatchtype_wage!=. | othermismatch==1

*Code whether financial respondent
gen finresp=0
replace finresp=1 if pn_fin==pn

gen finresp_nonprox=finresp
replace finresp_nonprox=0 if proxy==1

gen proxdum=.
replace proxdum=1 if finresp_nonprox==0
replace proxdum=0 if finresp_nonprox==1

*Code business assets 
gen busassetsdum=.
replace busassetsdum=0 if hh_bus_assets==0
replace busassetsdum=1 if hh_bus_assets>0 & hh_bus_assets!=.

*Define roles and clean outcomes
gen role_char=.
replace role_char=0 if working==1 & selfemp==0
replace role_char=1 if pred==3
replace role_char=2 if pred==2 
replace role_char=3 if pred==1
replace role_char=4 if working==0

forvalues i = 0/4 {
	gen role_char_`i'=0
	replace role_char_`i'=1 if role_char==`i'
}

gen role_char_5=0
replace role_char_5=1 if role_char==2 | role_char==3

gen role_char_se=0
replace role_char_se=1 if role_char==1 | role_char==2 | role_char==3

*Retirement status
gen saysretirementstatus=0 if saysatallretired==0 & saysfullyretired==0
replace saysretirementstatus=1 if saysatallretired==1 & saysfullyretired==0
replace saysretirementstatus=2 if saysatallretired==1 & saysfullyretired==1

gen saysretirementstatus2=saysretirementstatus
replace saysretirementstatus2=1 if saysretirementstatus==2

gen sayspartlyretired=saysatallretired
replace sayspartlyretired=0 if saysfullyretired==1

gen age_planstopwork=whenplanstostopwork-rabyear 
gen age_thinkstopwork=whenthinkswillstopwork-rabyear

local phys = "jphys jlift jstoop jsight jstres"

foreach v in selfreportedhealth `phys' {
	gen `v'_dum=`v'
	replace `v'_dum=0 if `v'==3 | `v'==4 | `v'==5
	replace `v'_dum=1 if `v'==2
}

*Age group variables
gen age_gp=.
replace age_gp=1 if age>=50 &age<55
replace age_gp=2 if age>=55 &age<60
replace age_gp=3 if age>=60 &age<65
replace age_gp=4 if age>=65 &age<70
replace age_gp=5 if age>=70 &age<75
replace age_gp=6 if age>=75 &age<80

*Abraham age group variables
gen age_gp_abr=.
replace age_gp_abr=1 if age>=53 &age<59
replace age_gp_abr=2 if age>=59 &age<65
replace age_gp_abr=3 if age>=65 &age<71
replace age_gp_abr=4 if age>=71 &age<78

*Investment income
egen investment_income=rowtotal(ibndin ichkin icdin)

*Scale and deflate HRS dollar amounts
foreach v in income pensionamt wealth savings hh_bus_assets investment_income {
	gen `v'_thou_real=(`v'/1000)*cpi_2016
	drop `v'
	rename `v'_thou_real `v'
}

*Scale and deflate SE and E dollar amounts
foreach v in se_inc2 wage_inc2 tot_se_earn_lcy non_se_earn_lcy{
	gen `v'_thou_real=(`v'/1000)*cpi_2016
	*gen `v'_thou_real_nz=`v'_thou_real
	*replace `v'_thou_real_nz=. if `v'_thou_real_nz==0
	*drop `v'
	*rename `v'_thou_real `v'
}

*Limit sample to individuals with nonmissing SE and E reports
gen insamp=0
replace insamp=1 if any_hrs_e!=. & any_hrs_se!=.

*Years since entered the survey
gen yrssinceenter=iwyear-yrenter

*Interview month
gen intmonth=iwmonth if waveyr==iwyear
replace intmonth=iwmonth+12 if waveyr==iwyear-1
replace intmonth=iwmonth+24 if waveyr==iwyear-2

*Industry
gen industry=jcindb
replace industry=. if industry>.
replace industry=jcindc if industry==. & jcindc<.
gen industry_flag=1 if industry==. & jcind<. & insamp==1 & waveyr>=2004 & waveyr<=2016 & wtcrnh>0 & wtcrnh!=. & (merge_ser_lcy==3 | merge_der_lcy==3) & sswgts_ev_scaled!=.

*Education
gen educat=.
replace educat=0 if educyr<=12
replace educat=1 if educyr>12 & educyr~=.

*Race
gen racecat=.
replace racecat=0 if white==1 & hispanic==0
replace racecat=1 if black==1 & hispanic==0
replace racecat=2 if hispanic==1
replace racecat=3 if racecat==. & otherrace==1

gen whitenh=0
replace whitenh=1 if white==1 & hispanic==0
gen blacknh=0
replace blacknh=1 if black==1 & hispanic==0

*Resident Children
gen residentchildrendum=numresidentchildren
replace residentchildrendum=1 if residentchildrendum>=1 & residentchildrendum!=.

*Non-financial Respondent
gen nonfinresp=.
replace nonfinresp=1 if finresp==0
replace nonfinresp=0 if finresp==1

*Harmonize HRS SE income amount
replace se_inc2_thou_real=0 if se_inc2<400

*Total income
gen totinc_ssa_nom=tot_se_earn_lcy+non_se_earn_lcy
gen totinc_hrs_nom=se_inc2+wage_inc2 
gen totinc_ssa=tot_se_earn_lcy_thou_real+non_se_earn_lcy_thou_real 
gen totinc_hrs=se_inc2_thou_real+wage_inc2_thou_real 

*Taxable Maximums
gen taxmax=0
replace taxmax=1 if (tot_se_earn_flag_lcy==1 | non_se_earn_flag_lcy==1) & lcy==2003 & EARN_lcy==87000
replace taxmax=1 if (tot_se_earn_flag_lcy==1 | non_se_earn_flag_lcy==1) & lcy==2004 & EARN_lcy==87900
replace taxmax=1 if (tot_se_earn_flag_lcy==1 | non_se_earn_flag_lcy==1) & lcy==2005 & EARN_lcy==90000
replace taxmax=1 if (tot_se_earn_flag_lcy==1 | non_se_earn_flag_lcy==1) & lcy==2006 & EARN_lcy==94200
replace taxmax=1 if (tot_se_earn_flag_lcy==1 | non_se_earn_flag_lcy==1) & lcy==2007 & EARN_lcy==97500
replace taxmax=1 if (tot_se_earn_flag_lcy==1 | non_se_earn_flag_lcy==1) & lcy==2008 & EARN_lcy==102000
replace taxmax=1 if (tot_se_earn_flag_lcy==1 | non_se_earn_flag_lcy==1) & lcy==2009 & EARN_lcy==106800
replace taxmax=1 if (tot_se_earn_flag_lcy==1 | non_se_earn_flag_lcy==1) & lcy==2010 & EARN_lcy==106800
replace taxmax=1 if (tot_se_earn_flag_lcy==1 | non_se_earn_flag_lcy==1) & lcy==2011 & EARN_lcy==106800
replace taxmax=1 if (tot_se_earn_flag_lcy==1 | non_se_earn_flag_lcy==1) & lcy==2012 & EARN_lcy==110100
replace taxmax=1 if (tot_se_earn_flag_lcy==1 | non_se_earn_flag_lcy==1) & lcy==2013 & EARN_lcy==113700
replace taxmax=1 if (tot_se_earn_flag_lcy==1 | non_se_earn_flag_lcy==1) & lcy==2014 & EARN_lcy==117000
replace taxmax=1 if (tot_se_earn_flag_lcy==1 | non_se_earn_flag_lcy==1) & lcy==2015 & EARN_lcy==118500
replace taxmax=1 if (tot_se_earn_flag_lcy==1 | non_se_earn_flag_lcy==1) & lcy==2016 & EARN_lcy==118500

*SSA top-coded
gen ssatopcoded_lcy=0
replace ssatopcoded_lcy=1 if (se_topcoded_lcy>=1 & se_topcoded_lcy!=.) | (non_se_topcoded_lcy>=1 & non_se_topcoded_lcy!=.) | taxmax==1 
replace tot_se_earn_lcy_thou_real=. if ssatopcoded_lcy==1
replace non_se_earn_lcy_thou_real=. if ssatopcoded_lcy==1

*Create variables for imputation/missing income
gen se_inc2_thou_real_noimp=se_inc2_thou_real
replace se_inc2_thou_real_noimp=. if inlist(ifse,2,3,4,5)
gen wage_inc2_thou_real_noimp=wage_inc2_thou_real
replace wage_inc2_thou_real_noimp=. if inlist(ifwage,2,3,4,5)

gen seincmiss=se_inc2_thou_real-tot_se_earn_lcy_thou_real
gen wsincmiss=wage_inc2_thou_real-non_se_earn_lcy_thou_real
gen totincmiss=(se_inc2_thou_real+wage_inc2_thou_real)-(tot_se_earn_lcy_thou_real+non_se_earn_lcy_thou_real) 

gen seincmiss_noimp=se_inc2_thou_real_noimp-tot_se_earn_lcy_thou_real
gen wsincmiss_noimp=wage_inc2_thou_real_noimp-non_se_earn_lcy_thou_real
gen totincmiss_noimp=(se_inc2_thou_real_noimp+wage_inc2_thou_real_noimp)-(tot_se_earn_lcy_thou_real+non_se_earn_lcy_thou_real) 

gen totinc_hrs_noimp=se_inc2_thou_real_noimp+wage_inc2_thou_real_noimp 
gen noimpute=1 if se_inc2_thou_real_noimp!=. & wage_inc2_thou_real_noimp!=.

*Create conditional earnings variables
foreach v in se_inc2_thou_real_noimp wage_inc2_thou_real_noimp totinc_hrs_noimp tot_se_earn_lcy_thou_real non_se_earn_lcy_thou_real totinc_ssa {
	gen `v'_cond=`v'
	replace `v'_cond=. if `v'==0
}

*HRS income quartiles
xtile quartile_hrs = se_inc2_thou_real_noimp [aw=sswgts_ev_scaled] if noimpute==1 & insamp==1 & waveyr>=2004 & waveyr<=2016 & wtcrnh>0 & wtcrnh!=. & (merge_ser_lcy==3 | merge_der_lcy==3) & sswgts_ev_scaled!=. ///
& (se_inc2_thou_real_noimp>0 & se_inc2_thou_real_noimp!=.), nq(4)

*Define SSA total income percentiles for figures
xtile percentile_20 = totinc_ssa [aw=sswgts_ev_scaled] if noimpute==1 & insamp==1 & waveyr>=2004 & waveyr<=2016 & wtcrnh>0 & wtcrnh!=. & (merge_ser_lcy==3 | merge_der_lcy==3) & sswgts_ev_scaled!=. ///
& ((totinc_ssa>0 & totinc_ssa!=.) | (totinc_hrs>0 & totinc_hrs!=.)), nq(20)

*Define SSA SE income percentiles for figures
xtile percentile_se_20 = tot_se_earn_lcy_thou_real [aw=sswgts_ev_scaled] if noimpute==1 & insamp==1 & waveyr>=2004 & waveyr<=2016 & wtcrnh>0 & wtcrnh!=. & (merge_ser_lcy==3 | merge_der_lcy==3) & sswgts_ev_scaled!=. ///
& (tot_se_earn_lcy_thou_real>0 & tot_se_earn_lcy_thou_real!=.), nq(20)

*Clean SE profit
gen se_profit_total=.
replace se_profit_total=se_profit_amt if se_profit_interval==6
replace se_profit_total=se_profit_amt*24 if se_profit_interval==5
replace se_profit_total=se_profit_amt*12 if se_profit_interval==4
replace se_profit_total=se_profit_amt*26 if se_profit_interval==3
replace se_profit_total=se_profit_amt*52 if se_profit_interval==2

replace se_profit_400=1 if se_profit_amt>=400 & se_profit_amt!=. 

*Save
save se_analysis_data_cleaned_1996_2018, replace

 
